public class SearchCatalogForLeadCtlr{
    // Lead record
    public final Lead leadRec;
    // Constructor
    public SearchCatalogForLeadCtlr(ApexPages.StandardController stdCon){
        this.leadRec = (Lead)stdCon.getRecord();
        leadRecordTypeId = ((String)leadRec.RecordTypeId).subString(0, 15);
        List<Lead> listLead = [Select Campus__c, Program__c From Lead Where Id = :this.leadRec.Id];
        if (listLead.size() > 0){
            strCampusId = listLead[0].Campus__c;
            strProgramId = listLead[0].Program__c;
            if (leadRecordTypeId != null && leadRecordTypeId != ''){
                //catalogRecordTypeName = mapLeadToCatalogRecordType.get(leadRecordTypeId);
                catalogRecordTypeName = mapLeadToCatalogRecordType.get(mapLeadRecordTypes.get(leadRecordTypeId));
                if (strCampusId != null && strCampusId != '' && strProgramId != null && strProgramId != ''){
                    Integer nCount = [Select Count() from Catalog__c Where Program__c = :strProgramId AND 
                                        //Campus__c = :strCampusId AND RecordTypeId = :catalogRecordTypeName AND 
                                        Campus__c = :strCampusId AND RecordType.DeveloperName = :catalogRecordTypeName AND 
                                        Active__c = true];
                    if (nCount == 0){
                        strCampusId = '';
                        strProgramId = '';
                    }
                }
            }        
        }
        bClosePage = false;
    }

    public Boolean bClosePage { get; set; }    
    // Record Id Parameter from Lead detail page
    public String leadRecordTypeId{ get; set; }
    public String catalogRecordTypeName { get; set; }
    
    // Map of Lead record types to Catalog record Types
    public Map<String, String> mapLeadToCatalogRecordType{
        get{
            if (mapLeadToCatalogRecordType == null){
                mapLeadToCatalogRecordType = new Map<String, String>();
                List<LeadToCatalogRecordTypes__c> listCS = LeadToCatalogRecordTypes__c.getAll().values();
                System.debug('====Custom Settings size: ' + listCS.size());
                for(Integer nCount = 0; nCount < listCS.size(); nCount++){
                    mapLeadToCatalogRecordType.put(listCS[nCount].LeadRecordType__c, listCS[nCount].CatalogRecordType__c);
                }
            }
            return mapLeadToCatalogRecordType;
        }
        set;
    }
    
    // Map of Lead record type ids to record type name
    public Map<String, String> mapLeadRecordTypes{
        get{
            if (mapLeadRecordTypes == null){
                mapLeadRecordTypes = new Map<String, String>();
                /*List<Schema.RecordTypeInfo> listRecTypes = Lead.sObjectType.getDescribe().getRecordTypeInfos();
                for (Schema.RecordTypeInfo rti : listRecTypes){
                    mapLeadRecordTypes.put(rti.getRecordTypeId(), rti.getName());
                }*/
                for(List<RecordType> listRecType : [Select Id, DeveloperName From RecordType Where SObjectType = 'Lead']){
                    for(RecordType rt : listRecType){
                        System.debug('----Lead Record Type: ' + rt.DeveloperName);
                        String strId = rt.Id;
                        mapLeadRecordTypes.put(strId.subString(0, 15), rt.DeveloperName);
                    }
                }
            }
            return mapLeadRecordTypes;
        }
        set;
    }
    
    // Campus selected by user
    public String strCampusId{ get; set; }
    
    // Campus pick list values
    public List<SelectOption> listCampus{
        get{
            if (listCampus == null){
                listCampus = new List<SelectOption>();
                if (leadRecordTypeId == null || leadRecordTypeId == ''){
                    leadRecordTypeId = ((String)this.leadRec.RecordTypeId).subString(0, 15);
                }
                if (leadRecordTypeId == null || leadRecordTypeId == ''){
                    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, 'Invalid Lead record type id: ' + leadRecordTypeId));
                    return listCampus;
                }
                //String strCatalogRecordTypeName = leadToCatalogRecordType.CatalogRecordType__c;
                //String strCatalogRecordTypeName = mapLeadToCatalogRecordType.get(leadRecordTypeId);
                String strCatalogRecordTypeName = mapLeadToCatalogRecordType.get(mapLeadRecordTypes.get(leadRecordTypeId));
                if (strCatalogRecordTypeName == null || strCatalogRecordTypeName == ''){
                    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, 'Valid Catalog record types not found for lead record type id: ' + leadRecordTypeId));
                    return listCampus;
                }
                listCampus.add(new SelectOption('', System.Label.NoneOption));
                Map<String, String> mapIds = new Map<String, STring>();
                for (List<Catalog__c> listCatalog : [Select Campus__r.Id, Campus__r.Name, Program__c From Catalog__c 
                                                        Where Active__c = true AND 
                                                        //RecordTypeId = :strCatalogRecordTypeName]){
                                                        RecordType.DeveloperName = :strCatalogRecordTypeName Order By Campus__r.Name]){
                    for (Catalog__c ct : listCatalog){
                        if (strProgramId != null && strProgramId != ''){
                            if (strProgramId == ct.Program__c){
                                if (mapIds.containsKey(ct.Campus__r.Id) == false){
                                    listCampus.add(new SelectOption(ct.Campus__r.Id, ct.Campus__r.Name));
                                    mapIds.put(ct.Campus__r.Id, '');
                                }
                            }
                        }
                        else{
                            if (ct.Campus__c != null && mapIds.containsKey(ct.Campus__r.Id) == false){
                                listCampus.add(new SelectOption(ct.Campus__r.Id, ct.Campus__r.Name));
                                mapIds.put(ct.Campus__r.Id, '');
                            }
                        }
                    }
                }
            }
            return listCampus;
        }
        set;
    }

    //Program selected by user
    public String strProgramId{ get; set; }

    // Program pick list values
    public List<SelectOption> listProgram{
        get{
            if (listProgram == null){
                listProgram = new List<SelectOption>();
                if (leadRecordTypeId == null || leadRecordTypeId == ''){
                    leadRecordTypeId = ((String)this.leadRec.RecordTypeId).subString(0, 15);
                }
                if (leadRecordTypeId == null || leadRecordTypeId == ''){
                    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, 'Invalid Lead record type id: ' + leadRecordTypeId));
                    return listProgram;
                }
                //String strCatalogRecordTypeName = leadToCatalogRecordType.CatalogRecordType__c;
                //String strCatalogRecordTypeName = mapLeadToCatalogRecordType.get(leadRecordTypeId);
                String strCatalogRecordTypeName = mapLeadToCatalogRecordType.get(mapLeadRecordTypes.get(leadRecordTypeId));
                if (strCatalogRecordTypeName == null || strCatalogRecordTypeName == ''){
                    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, 'Valid Catalog record types not found for lead record type id: ' + leadRecordTypeId));
                    return listProgram;
                }
                listProgram.add(new SelectOption('', System.Label.NoneOption));
                Map<String, String> mapIds = new Map<String,String>();
                for (List<Catalog__c> listCatalog : [Select Program__r.Id, Program__r.Name, Campus__c From Catalog__c 
                                                        Where Active__c = true AND 
                                                        //RecordTypeId = :strCatalogRecordTypeName]){
                                                        RecordType.DeveloperName = :strCatalogRecordTypeName Order By Program__r.Name]){
                    for (Catalog__c ct : listCatalog){
                        if (strCampusID != null && strCampusId != ''){
                            if (strCampusId == ct.Campus__c){
                                if (mapIds.containsKey(ct.Program__r.Id) == false){
                                    listProgram.add(new SelectOption(ct.Program__r.Id, ct.Program__r.Name));
                                    mapIds.put(ct.Program__r.Id, '');
                                }
                            }
                        }
                        else{
                            if (mapIds.containsKey(ct.Program__r.Id) == false){
                                listProgram.add(new SelectOption(ct.Program__r.Id, ct.Program__r.Name));
                                mapIds.put(ct.Program__r.Id, '');
                            }
                        }
                    }
                }
            }
            return listProgram;
        }
        set;
    }
    
    // Change event action for Campus pick list value changed
    public PageReference changeCampus(){
        listProgram = null;
        List<SelectOption> listTemp = listProgram;
        return null;
    }
    
    // Change event action for Program pick list value changed
    public PageReference changeProgram(){
        listCampus = null;
        List<SelectOption> listTemp = listCampus;
        return null;
    }
    
    // Submit action for Catalog selection
    public Pagereference submitChange(){
        bClosePage = false;
        /*if (strCampusId == null || strCampusId == ''){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, 'Please select Campus from the list.'));
            return null;
        }
        if (strProgramId == null || strProgramId == ''){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, 'Please select Program from the list.'));
            return null;
        }*/
        
        // Set Catalog on Lead
        List<Catalog__c> listCatalog = new List<Catalog__c>();
        if (strCampusId != null && strCampusId != '' && strProgramId != null && strProgramId != ''){
            listCatalog = [Select Id From Catalog__c Where 
                                                Campus__c = :strCampusId AND Program__c = :strProgramId AND 
                                                //RecordTypeId = :catalogRecordTypeName];
                                                RecordType.DeveloperName = :catalogRecordTypeName];
        }
        /*if (listCatalog.size() <= 0){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, 'No Catalog found for selected Campus and Program'));
            return null;
        }*/
        bClosePage = true;
        List<Lead> listLd = [Select Id, Catalog__c From Lead Where Id = :leadRec.Id];
        if (listLd.size() == 1){
            if (listCatalog.size() > 0){
                listLd[0].Catalog__c = listCatalog[0].Id;
            }
            else{
                listLd[0].Catalog__c = null;
            }
            listLd[0].Campus__c = strCampusId;
            listLd[0].Program__c = strProgramId;
            update listLd;
        }
        //return new PageReference('/' + leadRec.Id);
        return null;
    }
    
    
}